package xnj.lazydog.btcontroller.ControlViews;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

import xnj.lazydog.btcontroller.R;

public class ScanView extends LazyDogView {

    Bitmap scan_devices;

    float deg=0;
    float scale = 1;


    public  boolean isGoing;

    float bmp_ratio = 0.6f;
    float label_ratio =0.2f;

    String label = "扫描";


    public ScanView(Context context,AttributeSet attrs) {
        super(context, attrs);

        p.setTextAlign(Paint.Align.CENTER);

    }

    float ph = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        width = getMeasuredWidth();
        height = getMeasuredHeight();



        if(isGoing) {
            ph += 0.03f;
            deg += ((360 * Math.sin(ph)) - deg) * 0.15f;
        }else
            deg *=0.85f;

        if(scale>1.01f)
            scale +=(1-scale)*0.1f;

        if(scan_devices==null)
            scan_devices = getBitmapScaled(R.drawable.scan_devices,(int)(width*bmp_ratio),(int)(height*bmp_ratio),BMP_MODE_WRAP);
        if(scan_devices!=null) {
            canvas.save();
                canvas.translate(width * 0.5f, height * 0.5f);
            canvas.rotate(deg);
            canvas.scale(scale,scale);
            canvas.drawBitmap(scan_devices, - scan_devices.getWidth()*0.5f, -scan_devices.getHeight()*0.5f, p);
            canvas.restore();
        }
        invalidate();
    }


    public void startScanning()
    {
        isGoing = true;
        scale = 1.3f;
    }
    public void stopScanning()
    {
        isGoing = false;
        ph = 0;
    }

}
